package com.tianpengtech.common.model.user;
import com.qiniu.util.Auth;
import com.tianpengtech.common.model.AbstractBaseModel;
import com.tianpengtech.common.model.authenticate.Authenticate;

/**
 *实体类名-用户表
 * fair_user
 *
 * @since Fri May 26 13:29:02 HKT 2017
 */
public class User extends AbstractBaseModel< User > {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	public static final User dao = new User();
	public static final String USER_CACHE_NAME="user";

	public static final String ID ="id"; //
	public static final String MOBILE ="mobile"; //手机号
	public static final String PASSWORD ="password"; //密码
	public static final String SALT ="salt"; //密码加密盐
	public static final String DELETETAG ="deletetag"; //删除标识
	public static final String STATUS ="status"; //状态
	public static final String PHOTO ="photo"; //用户头像
	public static final String CREATE_TIME ="create_time"; //创建时间
	public static final String USER_TYPE ="user_type"; //用户类型
	public static final String CODE ="code"; //邀请码
	public static final String REF_ID ="ref_id"; //邀请人

	public static final int DELETE_STATUS_YES=1;
	public static final int DELETE_STATUS_NO=0;

	//用户正常
	public static final Integer USER_STATUS_NORMAL = 1;
	//账户锁定
	public static final Integer USER_STATUS_LOCK = 3;
	//账户密码错误次数过多锁定
	public static final Integer USER_STATUS_PWD_ERROR_LOCK = 5;

	//默认头像
	public static final String DEFAULT_PHOTO ="/upload/avatar/x.jpg";

	//用户类型
	public static final Integer USER_TYPE_PROV = 1; //任务主
	public static final Integer USER_TYPE_FAIR = 2;	//网红


	//存放注册手机验证码的cacheName
	public static final String AUTH_CODE_CACHENAME = "authCode";

	/**
	 * 获取用户认证信息
	 * @return
	 */
	public Authenticate getAuth(){
		return Authenticate.dao.findFirst("select * from "+Authenticate.dao.tableName()+" where " +Authenticate.DELETETAG + "=? and "+Authenticate.USER_ID+"=?",0,get(ID));
	}


	/**
	 * 账户状态转换
	 * @return
	 */
	public String status(){
		String result="";
		if(getInt(STATUS)==USER_STATUS_NORMAL){
			result="正常使用";
		}else if(getInt(STATUS)==USER_STATUS_LOCK){
			result="帐号锁定";
		}else if(getInt(STATUS)==USER_STATUS_PWD_ERROR_LOCK){
			result="错误次数锁定";
		}else{
			result="未知状态";
		}
		return result;
	}


	@Override
	public String tableName() {
		return "fair_user";
	}




	/**
	 * 判断账号是否锁定
	 * @return
	 */
	public boolean isStatusLockId(){
		if (getInt(STATUS).intValue()==USER_STATUS_LOCK){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 判断账号是否因为密码错误次数过多而锁定
	 * @return
	 */
	public boolean isPwdErrorLockId(){
		if (getInt(STATUS).intValue()==USER_STATUS_PWD_ERROR_LOCK.intValue()){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 用户状态是否是正常
	 * @return
	 */
	public boolean isStatusOk(){
		if (getInt(STATUS).intValue()==USER_STATUS_NORMAL){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 删除密码和盐 属性
	 */
	public void removeSensitiveInfo(){
		remove(PASSWORD).remove(SALT);
	}

	public static void main(String[] args) {

	}

}